package com.example.ad_project;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.nus.iss.ad.dbhelper.AuthenticateDBHelper;
import com.nus.iss.ad.model.LoginModel;
import com.nus.iss.ad.model.LoginUserInfo;

public class MainActivity extends BaseActivity {
	
	
	EditText userName,password;
	   private CheckBox saveLoginCheckBox;
	    private SharedPreferences loginPreferences;
	    private SharedPreferences.Editor loginPrefsEditor;
	    private Boolean saveLogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        boolean fromLogOut=false;
        		 Bundle extras = getIntent().getExtras();
        if (extras != null)
        {
        	fromLogOut = extras.getBoolean("logout");
        	
        }
       
        
        userName  = (EditText) findViewById(R.id.editText1); 
    	password  = (EditText) findViewById(R.id.editText2);
    	
        saveLoginCheckBox = (CheckBox)findViewById(R.id.saveLoginCheckBox);
        loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
        loginPrefsEditor = loginPreferences.edit();
        if(fromLogOut){
    		loginPrefsEditor.clear();
    		loginPrefsEditor.commit();
    	}

        saveLogin = loginPreferences.getBoolean("saveLogin", false);
        if (saveLogin == true) {
        	userName.setText(loginPreferences.getString("username", ""));
        	password.setText(loginPreferences.getString("password", ""));
            saveLoginCheckBox.setChecked(true);
            
            //autologin
            if(!fromLogOut) login(null);
            
          
        }
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }
    
    public void login(View view) {
    	//Toast.makeText(getApplicationContext(), "login Clicked",
		//		Toast.LENGTH_LONG).show();
    	LoginModel loginModel=new LoginModel();
    	loginModel.setUsername(userName.getText().toString());
    	loginModel.setPassword(password.getText().toString());
    	loginModel.setRemember(false);
    	
    	AuthenticateDBHelper data=new AuthenticateDBHelper();
    	boolean resultMvc=data.login(loginModel);
    	 if(resultMvc){
	        	
    		   //Remember Me
    		 if (saveLoginCheckBox.isChecked()) {
                 loginPrefsEditor.putBoolean("saveLogin", true);
                 loginPrefsEditor.putString("username", loginModel.getUsername());
                 loginPrefsEditor.putString("password", loginModel.getPassword());
                 loginPrefsEditor.commit();
             } 
	        	
	        	boolean isEmployee=LoginUserInfo.currentUser.isEmployee();
	        	boolean isDeptHead=LoginUserInfo.currentUser.isDeptHead();
	        	if(isEmployee
	        			|| isDeptHead)
	        	{
	        		Intent i=new Intent(this,Dept_Activity.class);
	        	startActivity(i);
	        	}else{
	        		Intent i=new Intent(this,StoreActivity.class);
		        	startActivity(i);
	        	}
	        	
	        }else {
	        	showDialog("Invalid Username or Password.");
			}
    	 
    	
	}
    

    
}
